package com.hx.miniblog.util;

import com.hx.miniblog.util.constant.Constant;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

/**
 * @author wenhao hu
 * @title: Result
 * @projectName miniBlog
 * @description: TODO
 * @date 2021/12/2816:30
 */
@Data
@NoArgsConstructor
@ToString
public class Result<T> {
    private String code;
    private String message;
    private T data;
    private Boolean success;
    private long timestamp = System.currentTimeMillis();


    public Result(String code,String message) {
        this.code = code;
        this.message = message;
    }

    public static<T> Result<T> OK() {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(Constant.HTTP_CODE.OK);
        r.setMessage(Constant.MESSAGE.SUCCESS_MESSAGE);
        r.setData(null);
        return r;
    }
    public static<T> Result<T> OK(T data) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(Constant.HTTP_CODE.OK);
        r.setMessage(Constant.MESSAGE.SUCCESS_MESSAGE);
        r.setData(data);
        return r;
    }
    public static<T> Result<T> OK(String msg, T data) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(Constant.HTTP_CODE.OK);
        r.setMessage(msg);
        r.setData(data);
        return r;
    }
    public static<T> Result<T> error(String msg, T data) {
        Result<T> r = new Result<T>();
        r.setSuccess(false);
        r.setCode(Constant.HTTP_CODE.FAIL);
        r.setMessage(msg);
        r.setData(data);
        return r;
    }
    public static Result<Object> error(String code, String msg) {
        Result<Object> r = new Result<Object>();
        r.setCode(code);
        r.setMessage(msg);
        r.setSuccess(false);
        return r;
    }
    public Result<Object> error500(String code, String msg) {
        Result<Object> r = new Result<Object>();
        r.setCode(code);
        r.setMessage(msg);
        r.setSuccess(false);
        return r;
    }
}
